/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mantechhelpdesk.bean;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import mantechhelpdesk.dao.DbConnection;
import mantechhelpdesk.dao.DepartmentsDao;
import mantechhelpdesk.entity.Departments;

/**
 *
 * @author DinhThang
 */
public class DepartmentsBean {

    private Departments departments = null;
    private List<Departments> deparList;
    private DepartmentsDao departmentsDao;
    private Connection conn;

    public Departments getDepartments() {
        return departments;
    }

    public void setDepartments(Departments departments) {
        this.departments = departments;
    }

    public List<Departments> getDeparList() {
        return deparList;
    }

    public void setDeparList(List<Departments> deparList) {
        this.deparList = deparList;
    }

    /**
     * Creates a new instance of DepartmentsBean
     */
    public DepartmentsBean() {
        try {
            departmentsDao = new DepartmentsDao();
            departments = new Departments();
            conn = DbConnection.getConnection();
            deparList = departmentsDao.loadAll(conn);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public String addNew() {
        FacesContext ct = FacesContext.getCurrentInstance();
        try {
            if (departments.getDepartmentName().length() > 0) {
                departmentsDao.create(conn, departments);
                deparList = departmentsDao.loadAll(conn);
                ct.addMessage("", new FacesMessage("Department addnew Success!"));
            } else {
                ct.addMessage("", new FacesMessage("Required to input a Department name!"));
                return "addNewError";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            ct.addMessage("", new FacesMessage("Cannot addnew Department!"));
        }
        return "addNewSuccess";
    }
    
    public String update() {
        FacesContext ct = FacesContext.getCurrentInstance();
        try {
            if (departments.getDepartmentName().length() > 0) {
                departmentsDao.save(conn, departments);
                deparList = departmentsDao.loadAll(conn);
                ct.addMessage("", new FacesMessage("Department update Success!"));
            } else {
                ct.addMessage("", new FacesMessage("Required to input a Department name!"));
                return "departUpdateError";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            ct.addMessage("", new FacesMessage("Cannot update Department!"));
        }
        return "updateBackToDepartList";
    }

    public String delete() {
        FacesContext ct = FacesContext.getCurrentInstance();
        try {
            departmentsDao.delete(conn, departments);
            ct.addMessage("", new FacesMessage("Department delete Success!"));  
            deparList = departmentsDao.loadAll(conn);
        } catch (Exception ex) {
            ex.printStackTrace();
            ct.addMessage("", new FacesMessage("Cannot delete Department!"));
        }
        return "delBackToDepartList";
    }

    public String prepareData4Update() {
        try {
            FacesContext ct = FacesContext.getCurrentInstance();
            Map<String, String> map = ct.getExternalContext().getRequestParameterMap();
            String id = map.get("idParam");
            departments = departmentsDao.getObject(conn, Integer.parseInt(id));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "updateDepart";
    }

    public String prepareData4Delete() {
        try {
            FacesContext ct = FacesContext.getCurrentInstance();
            Map<String, String> map = ct.getExternalContext().getRequestParameterMap();
            String id = map.get("idParam");
            departments = departmentsDao.getObject(conn, Integer.parseInt(id));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "delDepart";
    }
}
